1
2
3
4
5
6
7
8
9 package ca.uhn.cache.impl;
10
11 import java.io.Serializable;
12 import java.lang.reflect.Method;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Date;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.jmock.Mock;
20 import org.jmock.MockObjectTestCase;
21 import org.jmock.core.Invocation;
22 import org.jmock.core.Stub;
23
24 import ca.uhn.cache.IDataItem;
25 import ca.uhn.cache.IQuery;
26 import ca.uhn.cache.IQueryResult;
27 import ca.uhn.cache.VolatilityEnum;
28 import ca.uhn.cache.exception.DataSourceException;
29
30
31 /***
32 * TODO complete javadoc for
33 *
34 * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
35 * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:46 $ by $Author: bryan_tripp $
36 */
37 public class MethodDataSourceTest extends MockObjectTestCase {
38
39
40
41 private MethodDataSource myMethodDataSource;
42
43 private Mock myInstanceWithMethodMock;
44
45 private String myParamValue;
46 private String myReturnedElement;
47
48 private Collection myReturnedCollection;
49 private Set myReturnedSet;
50 private String[] myReturnedArray;
51
52 private org.jmock.cglib.Mock myMethodDataSourceMock;
53
54 private Method myMethodReturningCollection;
55
56 private Method myMethodReturningSet;
57
58 private Method myMethodReturningArray;
59
60 static interface InstanceWithMethod {
61 public Collection methodReturningCollection( String theStringParam );
62 public String[] methodReturningArray( String theStringParam );
63 public Set methodReturningSet( String theStringParam );
64 }
65
66 /***
67 * jmock stub for the method AbstractMethodDataSourceHelper.buildQueryResult(...)
68 * and AbstractMethodDataSourceHelper.buildDataItem(...)
69 */
70 private static class AbstractMethodDataSourceHelperStub implements Stub {
71 private final AbstractMethodDataSourceHelper myDataSourceHelper;
72 /***
73 * @param theSession The session to be used when executing the <code>HibernateCallback</code>.
74 */
75 public AbstractMethodDataSourceHelperStub( AbstractMethodDataSourceHelper theDataSourceHelper ) {
76 myDataSourceHelper = theDataSourceHelper;
77 }
78 /***
79 * {@inheritDoc}
80 */
81 public Object invoke( Invocation theInvocation ) throws Throwable {
82 Object retVal;
83
84 if ( "buildQueryResult".equals( theInvocation.invokedMethod.getName() ) ) {
85 retVal = myDataSourceHelper.buildQueryResult( theInvocation.parameterValues.get(0) );
86 }
87 else if ( "buildDataItem".equals( theInvocation.invokedMethod.getName() ) ) {
88 retVal = myDataSourceHelper.buildDataItem( (Serializable) theInvocation.parameterValues.get(0) );
89 }
90 else {
91 throw new RuntimeException( "This stub only applies to the methods <code>buildQueryResult</code> " +
92 "and <code>buildDataItem</code>" );
93 }
94 return retVal;
95 }
96 /***
97 * {@inheritDoc}
98 */
99 public StringBuffer describeTo( StringBuffer theBuffer ) {
100 return new StringBuffer( "stub for the method AbstractMethodDataSourceHelper.buildQueryResult(...) and " +
101 "AbstractMethodDataSourceHelper.buildDataItem(...)" );
102 }
103 }
104
105
106
107
108 protected void setUp() throws Exception {
109 super.setUp();
110
111 myMethodDataSource = new MethodDataSource();
112
113 myInstanceWithMethodMock = mock( InstanceWithMethod.class, "myInstanceWithMethodMock" );
114 InstanceWithMethod instanceWithMethod = (InstanceWithMethod) myInstanceWithMethodMock.proxy();
115
116 Class instanceWithMethodClass = instanceWithMethod.getClass();
117
118 myMethodReturningCollection =
119 instanceWithMethodClass.getMethod( "methodReturningCollection", new Class[] { String.class } );
120 myMethodReturningSet =
121 instanceWithMethodClass.getMethod( "methodReturningSet", new Class[] { String.class } );
122 myMethodReturningArray =
123 instanceWithMethodClass.getMethod( "methodReturningArray", new Class[] { String.class } );
124
125 myMethodDataSourceMock =
126 new org.jmock.cglib.Mock( AbstractMethodDataSourceHelper.class, "myMethodDataSourceMock" );
127 final AbstractMethodDataSourceHelper methodDataSourceHelper =
128 (AbstractMethodDataSourceHelper) myMethodDataSourceMock.proxy();
129
130 myMethodDataSourceMock.expects( once() )
131 .method("buildQueryResult")
132 .with( isA( Object.class ))
133 .will( new AbstractMethodDataSourceHelperStub(
134 new AbstractMethodDataSourceHelper() {
135 public IQuery buildProjection( Object theObj ) {
136 return methodDataSourceHelper.buildProjection(theObj);
137 }
138 public Object[] buildArguments( IQuery theQuery ) {
139 return methodDataSourceHelper.buildArguments(theQuery);
140 }
141 public String buildId( Serializable theObj ) {
142 return methodDataSourceHelper.buildId( theObj );
143 }
144 public VolatilityEnum getVolatility( Serializable theObj ) {
145 return methodDataSourceHelper.getVolatility( theObj );
146 }
147 public Date getLastUpdateTime( Serializable theObj)
148 {
149 return methodDataSourceHelper.getLastUpdateTime(theObj);
150 }
151
152 } ) );
153
154 myParamValue = "paramValue";
155
156 myReturnedElement = "returnedElement";
157
158 myReturnedCollection = new ArrayList();
159 myReturnedCollection.add( myReturnedElement );
160
161 myReturnedSet = new HashSet();
162 myReturnedSet.add( myReturnedElement );
163
164 myReturnedArray = new String[] { myReturnedElement };
165
166 myMethodDataSource.setInstance(instanceWithMethod);
167 myMethodDataSource.setMethodDataSourceHelper(methodDataSourceHelper);
168 }
169
170
171
172
173 protected void tearDown() throws Exception {
174 super.tearDown();
175 }
176
177 /***
178 * @throws DataSourceException ...
179 */
180 public void testMethodReturningCollection1() throws DataSourceException {
181
182 myMethodDataSource.setMethod( myMethodReturningCollection );
183
184 Query query = new Query();
185
186 Query projection = new Query();
187
188 Date date = new Date();
189
190 myInstanceWithMethodMock.expects( once() )
191 .method( "methodReturningCollection" )
192 .with( eq( myParamValue ) )
193 .will( returnValue( myReturnedCollection ) );
194
195 myMethodDataSourceMock.expects( once() )
196 .method( "buildArguments" )
197 .with( eq( query ) )
198 .will( returnValue( new Object[] { myParamValue } ) );
199
200 myMethodDataSourceMock.expects( once() )
201 .method( "buildProjection" )
202 .with( eq( myReturnedElement ) )
203 .will( returnValue( projection ) );
204
205 myMethodDataSourceMock.expects( once() )
206 .method( "buildId" )
207 .with( eq( myReturnedElement ) )
208 .will( returnValue( "id" ) );
209
210 myMethodDataSourceMock.expects( once() )
211 .method( "getVolatility" )
212 .with( eq( myReturnedElement ) )
213 .will( returnValue( VolatilityEnum.STABLE ) );
214
215 myMethodDataSourceMock.expects( once() )
216 .method( "getLastUpdateTime" )
217 .with( eq( myReturnedElement ) )
218 .will( returnValue( date ) );
219
220 IQueryResult expectedResult = new QueryResult();
221
222 IDataItem dataItem = new DataItem( "id", myReturnedElement, projection, VolatilityEnum.STABLE, date );
223 expectedResult.add(dataItem);
224
225 IQueryResult actualResult = myMethodDataSource.execute( new IQuery[] { query } );
226
227 assertEquals( expectedResult, actualResult );
228 }
229
230 /***
231 * @throws DataSourceException ...
232 */
233 public void testMethodReturningSet1() throws DataSourceException {
234
235 myMethodDataSource.setMethod( myMethodReturningSet );
236
237 Query query = new Query();
238
239 Query projection = new Query();
240
241 Date date = new Date();
242
243 myInstanceWithMethodMock.expects( once() )
244 .method( "methodReturningSet" )
245 .with( eq( myParamValue ) )
246 .will( returnValue( myReturnedSet ) );
247
248 myMethodDataSourceMock.expects( once() )
249 .method( "buildArguments" )
250 .with( eq( query ) )
251 .will( returnValue( new Object[] { myParamValue } ) );
252
253 myMethodDataSourceMock.expects( once() )
254 .method( "buildProjection" )
255 .with( eq( myReturnedElement ) )
256 .will( returnValue( projection ) );
257
258 myMethodDataSourceMock.expects( once() )
259 .method( "buildId" )
260 .with( eq( myReturnedElement ) )
261 .will( returnValue( "id" ) );
262
263 myMethodDataSourceMock.expects( once() )
264 .method( "getVolatility" )
265 .with( eq( myReturnedElement ) )
266 .will( returnValue( VolatilityEnum.STABLE ) );
267
268 myMethodDataSourceMock.expects( once() )
269 .method( "getLastUpdateTime" )
270 .with( eq( myReturnedElement ) )
271 .will( returnValue( date ) );
272
273 IQueryResult expectedResult = new QueryResult();
274
275 IDataItem dataItem = new DataItem( "id", myReturnedElement, projection, VolatilityEnum.STABLE, date );
276 expectedResult.add(dataItem);
277
278 IQueryResult actualResult = myMethodDataSource.execute( new IQuery[] { query } );
279
280 assertEquals( expectedResult, actualResult );
281 }
282
283 /***
284 * @throws DataSourceException ...
285 */
286 public void testMethodReturningArray1() throws DataSourceException {
287
288 myMethodDataSource.setMethod( myMethodReturningArray );
289
290 Query query = new Query();
291
292 Query projection = new Query();
293
294 Date date = new Date();
295
296 myInstanceWithMethodMock.expects( once() )
297 .method( "methodReturningArray" )
298 .with( eq( myParamValue ) )
299 .will( returnValue( myReturnedArray ) );
300
301 myMethodDataSourceMock.expects( once() )
302 .method( "buildArguments" )
303 .with( eq( query ) )
304 .will( returnValue( new Object[] { myParamValue } ) );
305
306 myMethodDataSourceMock.expects( once() )
307 .method( "buildProjection" )
308 .with( eq( myReturnedElement ) )
309 .will( returnValue( projection ) );
310
311 myMethodDataSourceMock.expects( once() )
312 .method( "buildId" )
313 .with( eq( myReturnedElement ) )
314 .will( returnValue( "id" ) );
315
316 myMethodDataSourceMock.expects( once() )
317 .method( "getVolatility" )
318 .with( eq( myReturnedElement ) )
319 .will( returnValue( VolatilityEnum.STABLE ) );
320
321 myMethodDataSourceMock.expects( once() )
322 .method( "getLastUpdateTime" )
323 .with( eq( myReturnedElement ) )
324 .will( returnValue( date ) );
325
326 IQueryResult expectedResult = new QueryResult();
327
328 IDataItem dataItem = new DataItem( "id", myReturnedElement, projection, VolatilityEnum.STABLE, date );
329 expectedResult.add(dataItem);
330
331 IQueryResult actualResult = myMethodDataSource.execute( new IQuery[] { query } );
332
333 assertEquals( expectedResult, actualResult );
334 }
335
336
337 }